<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>判断数组元素是否相等</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 400px;
            text-align: center;
        }

        h1 {
            color: #333;
            font-size: 24px;
        }

        .button-container {
            margin-top: 20px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }

        .result span {
            color: #4CAF50;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>判断数组元素是否相等</h1>
        <p>我们有一个数字数组(点击按钮后随机取值)，点击按钮查看数组中的所有元素是否都大于4。</p>

        <!-- 展示数组内容 -->
        <div class="button-container">
            <button onclick="checkArray()">检查数组</button>
        </div>

        <!-- 结果显示 -->
        <div class="result" id="result"></div>
    </div>

    <script>
        // allEqualBy 函数判断数组所有元素是否相等
        const allEqualBy = (arr, fn) => arr.every(item => item === fn(arr[0]));


        // 结果显示
        function checkArray() {
            const resultDiv = document.getElementById('result');

            const arr = Array.from({ length: 5 }).map(() => Math.floor(Math.random() * 10));

            const isEqual = allEqualBy(arr, item => item > 4);

            // 更新显示结果
            if (isEqual) {
                resultDiv.innerHTML = `${JSON.stringify(arr)}数组中的元素是否都大于4！<span>是的</span>`;
            } else {
                resultDiv.innerHTML = `${JSON.stringify(arr)}数组中的元素是否都大于4！<span>否</span>`;
            }
        }
    </script>
</body>

</html>